package com.commons.utils;

import org.springframework.data.domain.Page;

import java.util.Collections;
import java.util.List;

/**
 * 分页工具
 */
public class PageUtil extends cn.hutool.core.util.PageUtil {

    /**
     * List分页
     */
    public static <T> List<T> paging(int page,int size,List<T> list){
        int formIndex=page*size;
        int toIndex=page*size+size;
        if(formIndex>list.size()){
            return Collections.emptyList();
        }else if(toIndex>=list.size()){
            return list.subList(formIndex,list.size());
        }else {
            return list.subList(formIndex,toIndex);
        }
    }

    /**
     * page数据处理，预防redis反序列报错
     */
    public static <T> PageResult<T> toPage(Page<T> page){
        return new PageResult<>(page.getContent(),page.getTotalElements());
    }

    /**
     * 自定义分页
     */
    public static <T> PageResult<T> toPage(List<T> list, long totalElements) {
        return new PageResult<>(list, totalElements);
    }

    /**
     * 返回空数据
     */
    public static <T> PageResult<T> noData () {
        return new PageResult<>(null, 0);
    }

}
